<?php

namespace fast;

use think\Db;

use think\Exception;
use fast\Http;
/**
 * Http 请求类
 */
class Saobei
{

  
       
    public static function addOrder($order)
    {
    
        
        
        
        $url = "http://pay.lcsw.cn/lcsw/pay/open/minipretransfer";

        $data['pay_ver'] = "202";
        $data['pay_type'] = "200";
        $data['service_id'] = "020";
        $data['merchant_no'] = "865104816000387";
        $data['terminal_id'] = "16843171";
        $data['terminal_trace'] = $order['order_no'];
        $data['terminal_time'] = date('YmdHis');
        $data['sub_appid'] = 'wxa0e2e93181a0595c';
        $data['total_fee'] = $order['total_fee'];
        $data['notify_url'] = $order['notify_url'];
        $data['wx_code'] = $order['wx_code'];
      
        $key_sign = self::getSignContent($data);

        $key_sign = $key_sign."&access_token=819c173e351b469883a1943aceab1453";
        $key_sign = md5($key_sign);


        $data['key_sign'] = $key_sign;
        $sysParams = json_encode($data,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);



     
        $headers = ['Content-type: application/json'];
        $options = [
            CURLOPT_HTTPHEADER => $headers
        ];

        $response = Http::sendRequest($url, $sysParams, 'POST', $options);

       
      

  
        if ($response['ret']) {
            $msg = json_decode($response['msg'],true);

            if (isset($msg['return_code']) && $msg['return_code'] == '01') {
                return $msg;
            }else{
                return false;
            }
        }
     

    }
    


     public static function getSignContent($params) {
       
        ksort($params);
      
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
   
            if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
        }


     
        return $stringToBeSigned;
    }





}
